#!/bin/bash
{%- if hadoop_svc == 'namenode' %}
# chkconfig: 2345 95 6
{%- elif hadoop_svc == 'historyserver' %}
# chkconfig: 2345 99 6
{%- else %}
# chkconfig: 2345 96 6
{%- endif %}
# description: hadoop component init script
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


if [ -f /etc/init.d/functions ] ; then
        . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
        . /etc/rc.d/init.d/functions
else
        exit 0
fi

if [ $(id -un hdfs 2>/dev/null) ]
then
  HDFS_USER=hdfs
else
  HDFS_USER=hadoop
fi

if [ $(id -un mapred 2>/dev/null) ]
then
  MAPRED_USER=mapred
else
  MAPRED_USER=hadoop
fi

if [ $(id -un yarn 2>/dev/null) ]
then
  YARN_USER=yarn
else
  YARN_USER=hadoop
fi

[ -f /etc/profile.d/java.sh ] && . /etc/profile.d/java.sh
[ -f /etc/profile.d/hadoop.sh ] && . /etc/profile.d/hadoop.sh

script_name=$0
while [ -L $script_name ]
do
  script_name=$(readlink $script_name)
done
prog=${script_name/*\/hadoop-/}

case ${prog} in
    datanode )      
        DAEMON="DataNode"
        DAEMON_USER=$HDFS_USER
        ;;
    jobtracker )
        DAEMON="JobTracker"
        DAEMON_USER=$MAPRED_USER
        ;;
    nodemanager )
        DAEMON="NodeManager"
        DAEMON_USER=$YARN_USER
        DAEMON_SCRIPT="${HADOOP_PREFIX}/sbin/yarn-daemon.sh"
        ;;
    namenode )
        DAEMON="NameNode"
        DAEMON_USER=$HDFS_USER
        ;;
    secondarynamenode )
        DAEMON="SecondaryNameNode"
        DAEMON_USER=$HDFS_USER
        ;;
    tasktracker )
        DAEMON="TaskTracker"        
        DAEMON_USER=$MAPRED_USER
        ;;
    resourcemanager )
        DAEMON="ResourceManager"
        DAEMON_USER=$YARN_USER
        DAEMON_SCRIPT="${HADOOP_PREFIX}/sbin/yarn-daemon.sh"
        ;;
    historyserver )
        DAEMON="HistoryServer"
        DAEMON_USER=$HDFS_USER
        DAEMON_SCRIPT="${HADOOP_PREFIX}/sbin/mr-jobhistory-daemon.sh"
        ;;
    * )
        logger "No such hadoop node type (${prog}); stopping startup for ${script_name}"
        exit 1
        ;;
esac

if [ "" == "${DAEMON_SCRIPT}" ]
then
  if [ -x ${HADOOP_PREFIX}/bin/hadoop-daemon.sh ]; then
    DAEMON_SCRIPT=${HADOOP_PREFIX}/bin/hadoop-daemon.sh
  elif [ -x ${HADOOP_PREFIX}/sbin/hadoop-daemon.sh ]; then
    DAEMON_SCRIPT=${HADOOP_PREFIX}/sbin/hadoop-daemon.sh
  else
    echo "Cannot find daemon script in either $HADOOP_PREFIX/bin nor $HADOOP_PREFIX/sbin"
    exit 3
  fi
fi

start () {
        echo -n $"Starting ${prog}: "
        # start daemon
        su - ${DAEMON_USER} -c "PATH=${PATH}:${JAVA_HOME}/bin JAVA_HOME=${JAVA_HOME} ${DAEMON_SCRIPT} start ${prog}"
        RETVAL=$?
        echo
        [ ${RETVAL} = 0 ] && touch /var/lock/subsys/${prog}
        return ${RETVAL}
}

stop () {
        # stop daemon
        echo -n $"Stopping $prog: "
        su - ${DAEMON_USER} -c "PATH=${PATH}:${JAVA_HOME}/bin ${DAEMON_SCRIPT} stop ${prog}"
        RETVAL=$?
        echo
        [ ${RETVAL} = 0 ] && rm -f /var/lock/subsys/${prog}
}

restart() {
        stop
        sleep 5
        start
}

case $1 in
        start)
                start
        ;;
        stop)
                stop
        ;;
        restart)
                restart
        ;;
        condrestart)
                [ -f /var/lock/subsys/${prog} ] && restart || :
        ;;
        reload)
        echo "No supported"
        ;;
        status)
        if pgrep -f "${DAEMON}" &>/dev/null; then
            echo "${DAEMON} running"
            exit 0
        else
            echo "${DAEMON} stopped"
            exit 1
        fi
        ;;
        *)

        echo $"Usage: ${prog} {start|stop|restart|condrestart|reload|status}"
        exit 1
esac

exit ${RETVAL}

